package com.cheetah.listener;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Author Administrator
 * @Date 2019/7/30
 * @Version V1.0
 * @Description:
 **/
@Slf4j
public class CheetahCallback<T> implements Callback<T> {

    private CountDownLatch latch = new CountDownLatch(1);

    private AtomicBoolean flag = new AtomicBoolean(false);

    private T t;

    public CheetahCallback(){
    }

    @Override
    public void onEvent(T t) {
        this.t = t;
        latch.countDown();
    }

    @Override
    public void start() {
        try {

            latch.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            log.error(e.getMessage(),e);
        }
    }

    @Override
    public T getData() {
        if(latch.getCount() == 0) {
            return t;
        }else{
            while (true){
                if(latch.getCount() == 0){
                    return t;
                }
            }
        }
    }
}
